
/**
 *短信发送工具类，依赖JQ
**/
function PhoneCodeUtils(sendButton,msgHandler){
	this.isSendRequest = false;
	this.sendButton =  $("#"+sendButton);
	this.buttonShowText =this.sendButton.text();
	this.phoneCodeMsgHandler=msgHandler;
	
	this.sendPhoneCode = function(phoneNumId,type){
	    if(this.isSendRequest) return;
	    this.isSendRequest = true;
	    var phoneCodeUtils=this;
	    var phoneNum = $("#"+phoneNumId).val();
	    this.phoneCodeMsgHandler();
        jQuery.ajax({
            type: 'post',
            dataType: "json",
			data: "phoneNum="+phoneNum+"&type="+type,
            timeout: 2000,
			async: false,
            url: '/rpc/phone/sendPhoneCode.json',
            error:function(){
               phoneCodeUtils.sendRrror();
            },
            success:function(resp){
               phoneCodeUtils.sendSuccess(resp);
            }
        });
	};
	
	this.sendRrror = function(msg){
		var errorMsg="短信发送失败！请刷新页面后重试。";
		if(msg!=""){
		    errorMsg=msg;
		}
		this.isSendRequest=false;
		this.phoneCodeMsgHandler(errorMsg);
	};
	
	this.intervalHandler =function(time,iIntervalId){
	     if(time <= 0){
	        this.isSendRequest=false;
		    this.sendButton.text(this.buttonShowText); 
		    clearInterval(iIntervalId);
		 }else{
		    this.sendButton.text(time+"秒后重发"); 
		 }
	};
	
	this.sendSuccess = function (resp) {
	    if(!resp.hasError && resp.content && resp.content.resultCode=="0"){
		     var time=60;
		     var phoneCodeUtils=this;
			 this.sendButton.text(time+"秒后重发"); 
			 var iIntervalId =setInterval(function(){time--;phoneCodeUtils.intervalHandler(time,iIntervalId);},1000);
		}else{
		    var errorMsg="短信发送失败！请刷新页面后重试。";
		    if(!resp.hasError && resp.content && resp.content.resultMsg!=""){
			    errorMsg=resp.content.resultMsg;
			}
			if(!resp.hasError && resp.content && resp.content.sendSecond){
		        var time = 60 - resp.content.sendSecond;
		        var phoneCodeUtils=this;
			    this.sendButton.text(time+"秒后重发"); 
			    var iIntervalId =setInterval(function(){time--;phoneCodeUtils.intervalHandler(time,iIntervalId);},1000);
		        this.phoneCodeMsgHandler(errorMsg);
			}else{
		        this.sendRrror(errorMsg);
			}
		}
	};
}